package com.ymatou.iapi.usersafe.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.ArrayStack;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.ymatou.iapi.usersafe.parameter.GetIpAreasBean;
import com.ymatou.iapi.usersafe.service.GetIpAreasCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.IpChunHui;
import com.ymttest.database.sqlwapper.IPWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取ip地址
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetIpAreas{
	private static GetIpAreasBean getipareasBean;
	private static GetIpAreasCall getipareasCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取ip地址");
	}

	@Before
	public void caseUp() {
		getipareasBean=new GetIpAreasBean();
		getipareasCall=new GetIpAreasCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getipareasCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetIpAreas_001() {
		Logger.start(true,"获取ip地址,成功");
		try {
			List<String> ipList=new ArrayList<>();
			ipList.add(TestDataManager.getRandomIp());
			getipareasBean.setIpList(ipList);
			getipareasCall.setData(getipareasBean);
			getipareasCall.callService();			
			Logger.verifyEquals("true", getipareasCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getipareasCall.getString("ErrorCode"), "验证ErrorCode");
			IPWapper ipw=new IPWapper();
			getipareasCall.getJsonObject().get("Result").getAsJsonArray().forEach(jsone->{
				String ip=jsone.getAsJsonObject().get("ip").getAsString();
				Logger.verifyIsContains(ip,ipList.toString(), "验证ip");
				IpChunHui ipch = ipw.selectIpInfo(TestDataManager.converIpToLong(ip)).get(0);
				Logger.verifyEquals(ipch.getCountry(), jsone.getAsJsonObject().get("country").getAsString(), "验证country");
				Logger.verifyEquals(ipch.getProvince(), jsone.getAsJsonObject().get("province").getAsString(), "验证province");
				Logger.verifyEquals(ipch.getCity(), jsone.getAsJsonObject().get("city").getAsString(), "验证city");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetIpAreas_002() {
		Logger.start(true,"获取多个ip地址,成功");
		try {
			List<String> ipList=new ArrayList<>();
			for(int i=0;i<10;i++){
				ipList.add(TestDataManager.getRandomIp());
			}
			getipareasBean.setIpList(ipList);
			getipareasCall.setData(getipareasBean);
			getipareasCall.callService();			
			Logger.verifyEquals("true", getipareasCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getipareasCall.getString("ErrorCode"), "验证ErrorCode");
			IPWapper ipw=new IPWapper();
			getipareasCall.getJsonObject().get("Result").getAsJsonArray().forEach(jsone->{
				String ip=jsone.getAsJsonObject().get("ip").getAsString();
				Logger.verifyIsContains(ip,ipList.toString(), "验证ip");
				IpChunHui ipch = ipw.selectIpInfo(TestDataManager.converIpToLong(ip)).get(0);
				Logger.verifyEquals(ipch.getCountry(), jsone.getAsJsonObject().get("country").getAsString(), "验证country");
				Logger.verifyEquals(ipch.getProvince(), jsone.getAsJsonObject().get("province").getAsString(), "验证province");
				Logger.verifyEquals(ipch.getCity(), jsone.getAsJsonObject().get("city").getAsString(), "验证city");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIpAreas_003() {
		Logger.start(true,"不输入ipList失败");
		try {
			getipareasBean.setIpList(new ArrayList<>());
			getipareasCall.setData(getipareasBean);
			getipareasCall.callService();			
			Logger.verifyEquals("false", getipareasCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", getipareasCall.getString("ErrorCode"), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIpAreas_004() {
		Logger.start(true,"输入错误格式ipList失败");
		try {
			getipareasBean.setIpList(Arrays.asList("abc"));
			getipareasCall.setData(getipareasBean);
			getipareasCall.callService();			
			Logger.verifyEquals("true", getipareasCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", getipareasCall.getString("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(0,getipareasCall.getJsonObject().get("Result").getAsJsonArray().size(), "验证Result");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}